<?php
namespace app\gym\controller;
use app\common\controller\AdminBase;
/*私教合同管理*/
class GymPtOrderHistory extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();  
    }
	function index(){
		$shop_id=sp_get_current_shop_id();
		$this->assign("ptlist",db("GymEmployee")->where("status=1 and coash_type!=2 and shop_id=$shop_id")->select());
		echo $this->fetch();
	}	
	/*员工列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10061004");
		$this->assign("menus",$auth_group);
	}
	
	/*列表数据*/
	private function _ajax_index(){
		$shop_id=sp_get_current_shop_id();
		$card_no=input("card_no");
		$query=array(
					 'member_name'=>array("field" =>"c.name","operator" =>"="),
					 'mobile'=>array("field" =>"c.mobile","operator" =>"="),
					 'pt_id'=>array("field" =>"b.id","operator" =>"="),
					);
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		if(!empty($shop_id)){
			$where_and.=" and a.shop_id=$shop_id";
		}
		if(!empty($card_no)){
			$member_ids=db("GymOrder")->where("card_no='$card_no' and shop_id=$shop_id and is_delete=0")->column("member_id");
			if(!empty($member_ids)){
				$member_ids=implode(",", $member_ids);
				$where_and.=" and c.id in($member_ids)";	
			}
		}
		$field="a.id,a.course_name,a.status,a.type,a.course_count,a.price,a.surplus,a.end_date,c.name as member_name,c.mobile,b.name as pt_name,'' as card_no";
		$total=db("GymPtOrderHistory")->alias("a")->join([["m_gym_employee b","a.pt_id=b.id","left"],["m_gym_member c","a.member_id=c.id","left"]])->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("GymPtOrderHistory")->alias("a")->field($field)->join([["m_gym_employee b","a.pt_id=b.id","left"],["m_gym_member c","a.member_id=c.id","left"]])->where($where_and)->order("a.id desc")->limit($this->get_limit())->fetchsql(FALSE)->select();
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
}
